package com.example.user.app;

import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.support.v4.widget.CursorAdapter;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.google.inject.Inject;

import roboguice.activity.RoboActionBarActivity;
import roboguice.inject.InjectView;
import roboguice.util.Ln;

/**
 * Created by hzn on 14-6-26.
 */
public class MainActivity extends RoboActionBarActivity implements View.OnClickListener {
    private static final int CONTACT_LIST_QUERY_TOKEN = 9000;

    @InjectView(R.id.et_username)
    private EditText mUserNameView;
    @InjectView(R.id.bt_ok)
    private Button mOKView;
    @InjectView(R.id.lv_contacts)
    private ListView mContactsView;
    @Inject
    ContentResolver mContentResolver;

    CursorAdapter mContactsAdapter;

    ContactListQueryHandler mQueryHandler;

    String mUsername;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mOKView.setOnClickListener(this);

        mQueryHandler = new ContactListQueryHandler();

        mContactsAdapter = new ContactsAdapter(this, null);
        mContactsView.setAdapter(mContactsAdapter);
    }

    private boolean hasUsername() {
        return mUsername != null && !mUsername.equals("");
    }

    private void startQuery() {
        if (hasUsername()) {
            Cursor c = null;
            try {
                c = Contacts.startQuery(mContentResolver, mUsername);
            } catch (SQLiteException e) {
                Ln.e("query error ", e);
            }

            mQueryHandler.onQueryComplete(CONTACT_LIST_QUERY_TOKEN, null, c);
        }

    }

    @Override
    protected void onStart() {
        super.onStart();

        startQuery();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu optionMenu) {
        getMenuInflater().inflate(R.menu.main, optionMenu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.menu_add) {
            Ln.d("do something");
            ContentValues values = new ContentValues();
            values.put(Contacts.NAME, "hzn");
            values.put(Contacts.MOBILE_NUMBER, "18621666570");
            Contacts.addContact(mContentResolver, mUsername, values);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        String txt = mUserNameView.getText().toString();
        if (txt != null && !txt.equals("")) {
            mUsername = txt;

            startQuery();
        } else {
            Toast.makeText(this, R.string.data_must_not_empty, Toast.LENGTH_LONG).show();
        }
    }

    /**
     * The contact list query handler.
     */
    private final class ContactListQueryHandler extends AsyncQueryHandler {
        public ContactListQueryHandler() {
            super(mContentResolver);
        }

        @Override
        protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
            if (cursor == null) {
                mContactsAdapter.changeCursor(null);
                return;
            }

            switch (token) {
                case CONTACT_LIST_QUERY_TOKEN:
                    mContactsAdapter.changeCursor(cursor);
                    break;
                default:
                    throw new IllegalArgumentException("onQueryComplete called with unknown");
            }
        }
    }
}
